package com.wxh.multithread;

import java.util.concurrent.CountDownLatch;

/**
 * @ClassName: MyThread
 * @Description: CountDownLatch 测试
 * @Author wxh
 * @Date: 2022/4/12 16:06
 * @Version V1.0.0
 * @Since 1.8
 */
public class MyThread extends Thread {
    /**
     * 倒数计数器, 直到内部的state减到零, 才会执行下一个线程
     */
    private CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) {
        MyThread th = new MyThread();
        Thread t1 = new Thread(th, "Mythread");
        t1.start();
        System.out.println(Thread.currentThread().getName());
    }

    @Override
    public void run() {
        Mythread1 th2 = new Mythread1();
        Thread t2 = new Thread(th2, "Mythread1");
        t2.start();
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName());
    }

    class Mythread1 extends Thread {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
            countDownLatch.countDown();
        }
    }

}
